package cn.beyond.vehicleManagement.controller;
import cn.beyond.vehicleManagement.pojo.VehicleProcurement;
import cn.beyond.vehicleManagement.pojo.Vehicles;
import cn.beyond.vehicleManagement.service.UserService;
import cn.beyond.vehicleManagement.service.VehicleProcurementService;
import cn.beyond.vehicleManagement.util.PageUtil;
import com.alibaba.fastjson.JSON;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet(urlPatterns = {"/VehicleProcurementServlet"})
public class VehicleProcurementServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //定义全局变量
        VehicleProcurementService service=new VehicleProcurementService();
        String opr= req.getParameter("opr");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        VehicleProcurement vehicleProcurement=new VehicleProcurement();
        Map<String,Object> map = new HashMap<>();
        PrintWriter out= resp.getWriter();
        List<VehicleProcurement> vehicleProcurementList=new ArrayList<>();
        System.out.println(opr);
        switch (opr){
//            case "addvehicleProcurement":
//                boolean addflag= addUser(req,userService);
//                out.print(addflag);
//                break;
            //删除车辆采购记录
            case "deletevehicleProcurement":
                int delId=Integer.parseInt(req.getParameter("Id"));
                boolean delflag=false;
                int delcount=service.delete(delId,"id");
                System.out.println(delcount);
                if(delcount>0){
                    delflag=true;
                }
                out.print(delflag);
                break;
//            case "updatevehicleProcurement":
//                updateUser(req,userService,out);
//
//                break;
            case "selectvehicleProcurement":

                break;
                case "listvehicleProcurement":
                    //列举出采购列表
                    int startRow= Integer.parseInt(req.getParameter("startRow"));
                    int pageSize= Integer.parseInt(req.getParameter("pageSize"));
                    int totals= PageUtil.getPageCount("vehicle_procurement");
                    int totalpage=totals%pageSize==0?(totals/pageSize):(totals/pageSize+1);
                    List<Integer>totalinfo=new ArrayList<>();
                    totalinfo.add(totals);
                    totalinfo.add(totalpage);
                    Vehicles vehicles=new Vehicles();
                    vehicleProcurementList= PageUtil.getPageUtil(startRow,pageSize,"vehicle_procurement",vehicleProcurement);
                    List<Vehicles>vehiclesList=PageUtil.getPageUtil(startRow,pageSize,"vehicles",
                            vehicles);
                    List<Object>listListMap=new ArrayList<>();
                    listListMap.add(totalinfo);
                    listListMap.add(vehicleProcurementList);
                    listListMap.add(vehiclesList);
                String listJosn= JSON.toJSONString(listListMap);
                out.write(listJosn);
                break;

        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }

    /**
     * 新增车辆采购记录
     * @param req
     * @param service
     */
    public void addvehiclementProcurement(HttpServletRequest req,
                                          VehicleProcurementService service){

    }

    /**
     * 更新车辆采购记录
     * @param req
     * @param service
     */
    public void updatevehiclementProcurement(HttpServletRequest req,
                                             VehicleProcurementService service){

    }
}
